package com.nl.module_main.xiecheng

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.util.concurrent.Executor
import java.util.concurrent.Executors

/**
 * 作者: wangfei
 * 创建时期: 2022/12/1.
 * 包名: com.nl.module_main.xiecheng
 * 类名: NL_MedicaleEvaluation
 */
object Test03 {

    @JvmStatic
    fun main(args: Array<String>) {
        //test()
        test04()
    }

    private val mDispatcher = Executors.newCachedThreadPool().asCoroutineDispatcher()

    fun test() = runBlocking {
        val scope = CoroutineScope(mDispatcher)
        (1 .. 5).asFlow().onEach { println(it) }
            .onCompletion { mDispatcher.close() }
            .launchIn(scope)
    }

    fun test02() = runBlocking {
//        val scope = CoroutineScope(mDispatcher)
        (1 .. 5).asFlow().onEach {
            delay(100)
            println(it)
        }
            .flowOn(Dispatchers.IO)
            .onCompletion { mDispatcher.close() }
            .launchIn(this)

        (5 .. 10).asFlow().onEach {
            delay(200)
            println(it)
        }
            .onCompletion { mDispatcher.close() }
            .flowOn(Dispatchers.IO)
            .launchIn(this)

    }


    fun test03() = runBlocking {
        val flow = (1..9).asFlow()
        val flowB = flowOf("one", "two", "three","four","five", "six", "seven").onEach { delay(200) }
        flow.zip(flowB){
            a,b ->
            "$a and $b"
        }.collect{
            println(it)
        }
    }

    fun test04() = runBlocking {
        val flow = (1..9).asFlow()
        val flowB = flowOf("one", "two", "three","four","five", "six", "seven")
        flowOf(flow,flowB)
            .flattenConcat()
            .collect{
            println(it)
        }
    }



}